package com.icss.ah.categoryProduct.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.base.bean.SyncSelectTree;
import com.icss.ah.login.bean.UserInfoBean;

/**
 * 店铺审核Action
 * @author sw
 * @date 2016-10-09
 *
 */
@Controller
@Scope("prototype")
public class CategoryProductAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	// json串
	private Json jsonText;
	

	
	/**
	 * 平台商品页面
	 * @return
	 */
	public String toCategoryProductList(){
		return "toCategoryProductList";
	}
	
	/**
	 * 获取商品分类树
	 */
	public void getTree(){
		request = getRequest();
		//id 异步加载展开树时传到后台的id
		//tarEnt 入口编码，以编码为入口编码的节点作为根节点
		//ifAll 是否一次性展示所有数据，1是 0否
		String id = request.getParameter("id");
		id = id == null ? "" : id;
		String ifAll = request.getParameter("ifAll");
		ifAll = ifAll == null ? "0" : ifAll;
		SyncSelectTree selectTree = new SyncSelectTree();
		selectTree.setId(id);
		selectTree.setIfAll(ifAll);
		List<SyncSelectTree> list = categoryProductService.getTree(selectTree);
		for (SyncSelectTree sst : list) {
			sst.setOpen(true);
			if(sst.getIsParent() && "0".equals(sst.getpId())){
				sst.setNocheck(true);
			}
		}
		str2Resp(JSONArray.fromObject(list).toString());
	}
	
	/**
	 * 查询商品分类参数列表
	 */
	public void queryProductList(){
		request = this.getRequest();
		
		String categoryCode = request.getParameter("categoryCode");
		categoryCode = (categoryCode == null || "".equals(categoryCode)) ? "" : categoryCode;
		String productTypeCode = request.getParameter("productTypeCode");
		productTypeCode = (productTypeCode == null || "".equals(productTypeCode)) ? "" : productTypeCode;
		String proName = request.getParameter("proName");
		
		String result = "";
		try {
 			dataGridJson = new EasyuiDataGridJson();
			UserInfoBean userInfo=getUserInfo(getUserCodeFromSession());
			String shopId=userInfo.getShopId();
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("shopId", shopId);
			map.put("categoryCode", categoryCode);
			map.put("startNo",getStartIndexMysql());
			map.put("pageRows",getEndIndexMysql());
			map.put("productTypeCode",productTypeCode);
			map.put("proName",proName);
			List<Map> categoryList = new ArrayList();
			categoryList = categoryProductService.queryProductList(map);
			long sortCount = categoryProductService.queryProductCount(map);//查询总记录数
			dataGridJson.setRows(categoryList);
			dataGridJson.setTotal(sortCount);
			result = JSONObject.fromObject(dataGridJson).toString();
			infolog.info("CategorySetAction|店铺id：" + shopId + "，查询商品参数列表成功");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("CategorySetAction|查询商品参数列表失败：",e);
		} finally {
			str2Resp(result);
		}
	}	
	
	

	public Json getJsonText() {
		return jsonText;
	}

	public void setJsonText(Json jsonText) {
		this.jsonText = jsonText;
	}
	
}
